/*
	Some init functions
*/

var gameServer = top.location.host;
$ = document.getElementById;

var ENCODED_IMAGE_TYPE = 'ENCODED_IMAGE_TYPE'; 

function initIsMessageForTrade()	{
	// isMessageForTrade[x] defines that messageTypes[z] is for trading goods or something else		
	var isMessageForTrade = new Array(11);
	isMessageForTrade[0] = 0;
	isMessageForTrade[1] = 1;
	isMessageForTrade[2] = 1;	
	isMessageForTrade[3] = 1;
	isMessageForTrade[4] = 0;
	isMessageForTrade[5] = 0;
	isMessageForTrade[6] = 0;
	isMessageForTrade[7] = 0;
	isMessageForTrade[8] = 0;
	isMessageForTrade[9] = 0;
	isMessageForTrade[10] = 0;
	
	return isMessageForTrade;
}

function initDefaultOnClickEventRequiredForMessageTypeElement()	{
	// defines default onclick event to be generated for messageTypes[z]
	var res = new Array(11);
	res[0] = true;
	res[1] = true;
	res[2] = true;
	res[3] = true;
	res[4] = true;
	res[5] = true;
	res[6] = true;
	res[7] = true;
	res[8] = false;
	res[9] = true;
	res[10] = true;
	res[11] = true;
	
	return res;
}

function initResourceNamesForAdditionalParams()	{
	var res = new Object();
	res = {
		'WOOD' : '/skin/resources/icon_wood.gif' ,	
		'WINE' : '/skin/resources/icon_wine.gif' ,	
		'MARBLE' : '/skin/resources/icon_marble.gif' ,	
		'CRYSTAL' : '/skin/resources/icon_glass.gif' ,	
		'SULFUR' : '/skin/resources/icon_sulfur.gif'
	};
	
	res[getResourceNamesInLang('WOOD')] = '/skin/resources/icon_wood.gif'; 
	res[getResourceNamesInLang('WINE')] = '/skin/resources/icon_wine.gif';
	res[getResourceNamesInLang('MARBLE')] = '/skin/resources/icon_marble.gif';
	res[getResourceNamesInLang('CRYSTAL')] = '/skin/resources/icon_glass.gif';
	res[getResourceNamesInLang('SULFUR')] = '/skin/resources/icon_sulfur.gif';
	
	return res;
}

function initUserSettingsTable()	{	
	return {
		// name of the setting element : ( displayed tag type, tag attribute value definer )
		'CASE_SENSITIVE_MARKER_CHECK' : new Array('checkbox', 'checkbox') ,	
		'ADDITIONAL_MESSAGE_EDITOR_OPTIONS' : new Array('checkbox', 'checkbox') ,	
		'STARTING_DISPLAY_STATE_FOR_SPECIAL_MESSAGES_LIST' : new Array('checkbox', 'div') ,	
		'CHECK_ALLY_MESSAGES_WITHOUT_MARKER' : new Array('checkbox', 'checkbox') ,	
		'STARTING_DISPLAY_STATE_FOR_ADDRESS_BOOK' : new Array('checkbox', 'div') ,	
		'NUMBER_OF_MESSAGES_ON_PAGE' : new Array('select', 'int'),
		'LOCALIZED_DATEFORMAT' : new Array('checkbox', 'checkbox'),
		'UPDATE_USER_NAMES_IN_ADDRESSBOOK' : new Array('checkbox', 'checkbox'),
		'SELECTED_LANGUAGE' : new Array('select', 'string'),
		'CHANGE_MESSAGE_SUBJECT_WO_MARKER' : new Array('checkbox', 'checkbox')
	};
}



function initUserSettingValues()	{
	return {
		'CASE_SENSITIVE_MARKER_CHECK' : getUserSettingValue('CASE_SENSITIVE_MARKER_CHECK', true) ,	
				// If CASE_SENSITIVE_MARKER_CHECK variable is set to false, all string will converted to uppercase before searching to special marker
		'ADDITIONAL_MESSAGE_EDITOR_OPTIONS' : getUserSettingValue('ADDITIONAL_MESSAGE_EDITOR_OPTIONS', true) ,
				// If ADDITIONAL_MESSAGE_EDITOR_OPTIONS variable set to true, CLEARMARKER and CLEARMESSAGE options will be available
		'STARTING_DISPLAY_STATE_FOR_SPECIAL_MESSAGES_LIST' : getUserSettingValue('STARTING_DISPLAY_STATE_FOR_SPECIAL_MESSAGES_LIST', 'none') ,	
				// STARTING_DISPLAY_STATE_FOR_SPECIAL_MESSAGES_LIST variable sets the starting display state of the extra menu. Valid values are 'none' and 'block'	
		'CHECK_ALLY_MESSAGES_WITHOUT_MARKER' : getUserSettingValue('CHECK_ALLY_MESSAGES_WITHOUT_MARKER', true),
				// If CHECK_ALLY_MESSAGES_WITHOUT_MARKER is true, select the allymessages, put a tick in it's checkbox if no marker found in the message text..
		'STARTING_DISPLAY_STATE_FOR_ADDRESS_BOOK' : getUserSettingValue('STARTING_DISPLAY_STATE_FOR_ADDRESS_BOOK', 'none'),
		
		'NUMBER_OF_MESSAGES_ON_PAGE' : getUserSettingValue('NUMBER_OF_MESSAGES_ON_PAGE', 10),
		'LOCALIZED_DATEFORMAT' : getUserSettingValue('LOCALIZED_DATEFORMAT', false),
		'UPDATE_USER_NAMES_IN_ADDRESSBOOK' : getUserSettingValue('UPDATE_USER_NAMES_IN_ADDRESSBOOK', false),
		'SELECTED_LANGUAGE' : getUserSettingValue('SELECTED_LANGUAGE', 'dft' ),
		'CHANGE_MESSAGE_SUBJECT_WO_MARKER' : getUserSettingValue('CHANGE_MESSAGE_SUBJECT_WO_MARKER', false )
		
	};
}

function initSelectVectors()	{
	return {
		'NUMBER_OF_MESSAGES_ON_PAGE' : { 10 : '10', 20 : '20', 30 : '30', 40 : '40', 50 : '50', 60 : '60', 70 : '70', 80 : '80', 90 : '90', 100 : '100' },
		'SELECTED_LANGUAGE' : langList
	};
}

var messageMarkerSeparator = '\*\*\*';	// This variable sets the special marker separator, used by the replace function
var EXCHANGE_DIRECTION_ICON_PATH = '/skin/resources/icon_actionpoints.gif';
var ADD_USER_TO_ADDRESSBOOK_ICON_PATH = '/skin/icons/growth_positive.gif';
var REMOVE_USER_FROM_ADDRESSBOOK_ICON_PATH = '/skin/icons/growth_negative.gif';

var messageTypeKeyWords = new Array( 'MENTOR', 'CHANGE', 'SELL', 'BUY', 'HELP', 'CULTURAL', 'SENATE', 'SMILE', 'CUSTOM', 'VOTE', 'COMBATREPORT', 'BIRTHDAY' ) ;			
var resourceTypeKeyWords = new Array('WOOD', 'WINE', 'MARBLE', 'CRYSTAL', 'SULFUR' );

var isMessageForTrade = initIsMessageForTrade();
var resourceNamesForAdditionalParams = initResourceNamesForAdditionalParams();

function initDefaultSubjectIcons()	{	
	var res = new Object();
	
	res = { 
		'MENTOR' : '/skin/layout/bulb-on.gif' ,			
		'ELAD' : '/skin/resources/icon_gold.gif' ,
		'VESZ' : '/skin/resources/icon_gold.gif' ,
		'SEGITSEG' : '/skin/layout/shield-icon1.gif' ,	
		'KULTUR' : '/skin/museum/icon32_culturalgood.gif' ,
		'SENATE' : '/skin/layout/crown.gif' ,
		'MOSOLY' : '/skin/smilies/ecstatic.gif',
		'SZAVAZ'	: '/skin/resources/icon_population.gif',
			
		'SELL' : '/skin/resources/icon_gold.gif' ,
		'BUY' : '/skin/resources/icon_gold.gif' ,
		'HELP' : '/skin/layout/shield-icon1.gif' ,	
		'CULTURE' : '/skin/museum/icon32_culturalgood.gif' ,
		'SMILE' : '/skin/smilies/ecstatic.gif',
		'VOTE'	: '/skin/resources/icon_population.gif'			
	};
	
	res[CUSTOM_KEYWORD] = '/skin/layout/bulb-off.gif';
	res[CHANGE_KEYWORD] = '/skin/resources/icon_gold.gif';	
	res[HUN_CHANGE_KEYWORD] = '/skin/resources/icon_gold.gif';
	res[HUN_CUSTOM_KEYWORD] = '/skin/layout/bulb-off.gif';
	
// Delete the previous stuff in the first release in july	
	
	res[getKeywordsInLang('MENTOR')] = '/skin/layout/bulb-on.gif'; 
	res[getKeywordsInLang('SELL')] = '/skin/resources/icon_gold.gif';
	res[getKeywordsInLang('BUY')] = '/skin/resources/icon_gold.gif';
	res[getKeywordsInLang('CHANGE')] = '/skin/resources/icon_gold.gif';
	res[getKeywordsInLang('HELP')] = '/skin/layout/shield-icon1.gif';
	res[getKeywordsInLang('CULTURE')] = '/skin/museum/icon32_culturalgood.gif';
	res[getKeywordsInLang('SENATE')] = '/skin/layout/crown.gif';
	res[getKeywordsInLang('SMILE')] = '/skin/smilies/ecstatic.gif';
	res[getKeywordsInLang('VOTE')] = '/skin/resources/icon_population.gif';
	res[getKeywordsInLang('CUSTOM')] = '/skin/layout/bulb-off.gif';
	res[getKeywordsInLang('COMBATREPORT')] = '/skin/layout/icon-crossedswords.gif';
	res[getKeywordsInLang('BIRTHDAY')] = ENCODED_IMAGE_TYPE;
	
	return res;
}

function initEncodedImages()	{
	var res = new Object();
	res[getKeywordsInLang('BIRTHDAY')] = "data:image/jpeg,%FF%D8%FF%E0%00%10JFIF%00%01%01%01%00%60%00%60%00%00%FF%E1%00nExif%00%00II*%00%08%00%00%00%01%00i%87%04%00%01%00%00%00%1A%00%00%00" +
		"%00%00%00%00%01%00%86%92%02%00%3A%00%00%00%2C%00%00%00%00%00%00%00CREATOR%3A%20gd-jpeg%20v1.0%20(using%20IJG%20JPEG%20v62)%2C%20quality%20%3D%2090%0A%00%FF%DB%00C%00%08%06%06%07%0" +
		"6%05%08%07%07%07%09%09%08%0A%0C%14%0D%0C%0B%0B%0C%19%12%13%0F%14%1D%1A%1F%1E%1D%1A%1C%1C%20%24.'%20%22%2C%23%1C%1C(7)%2C01444%1F'9%3D82%3C.342%FF%DB%00C%01%09%09%09%0C%0B%0C%18%0D" +
		"%0D%182!%1C!22222222222222222222222222222222222222222222222222%FF%C0%00%11%08%00(%00(%03%01%22%00%02%11%01%03%11%01%FF%C4%00%1F%00%00%01%05%01%01%01%01%01%01%00%00%00%00%00%00%00%" +
		"00%01%02%03%04%05%06%07%08%09%0A%0B%FF%C4%00%B5%10%00%02%01%03%03%02%04%03%05%05%04%04%00%00%01%7D%01%02%03%00%04%11%05%12!1A%06%13Qa%07%22q%142%81%91%A1%08%23B%B1%C1%15R%D1%F0%24" +
		"3br%82%09%0A%16%17%18%19%1A%25%26'()*456789%3ACDEFGHIJSTUVWXYZcdefghijstuvwxyz%83%84%85%86%87%88%89%8A%92%93%94%95%96%97%98%99%9A%A2%A3%A4%A5%A6%A7%A8%A9%AA%B2%B3%B4%B5%B6%B7%B8%B" +
		"9%BA%C2%C3%C4%C5%C6%C7%C8%C9%CA%D2%D3%D4%D5%D6%D7%D8%D9%DA%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FF%C4%00%1F%01%00%03%01%01%01%01%01%01%01%01%01%00%00%00%00%" +
		"00%00%01%02%03%04%05%06%07%08%09%0A%0B%FF%C4%00%B5%11%00%02%01%02%04%04%03%04%07%05%04%04%00%01%02w%00%01%02%03%11%04%05!1%06%12AQ%07aq%13%222%81%08%14B%91%A1%B1%C1%09%233R%F0%15b" +
		"r%D1%0A%16%244%E1%25%F1%17%18%19%1A%26'()*56789%3ACDEFGHIJSTUVWXYZcdefghijstuvwxyz%82%83%84%85%86%87%88%89%8A%92%93%94%95%96%97%98%99%9A%A2%A3%A4%A5%A6%A7%A8%A9%AA%B2%B3%B4%B5%B6%" +
		"B7%B8%B9%BA%C2%C3%C4%C5%C6%C7%C8%C9%CA%D2%D3%D4%D5%D6%D7%D8%D9%DA%E2%E3%E4%E5%E6%E7%E8%E9%EA%F2%F3%F4%F5%F6%F7%F8%F9%FA%FF%DA%00%0C%03%01%00%02%11%03%11%00%3F%00%F7j(%A2%B8%CD%0F%" +
		"3F%F1'%8D%9BJ%F1%DE%9Fb%88%CDgnB%5E%10%3F%8AQ%F2%FEC%0D%EF%CDz%05p%3Fo%D3%EF%3E%23%DC%E92%E9%0Cf%DF%1C%B21%90%1D%CE%8B%F2HT%81%81%B4v'%A0%E3%AE%7B%EAn%9DZo%F7%8B%7DW%A1%BDZ%B4%A7%" +
		"18%AAj%CD%2B%3F6%14QE%23%02%BC%B2J%D0%99%22Ve%0CA%DB%8D%C3%07%07%B1%CF%D3%1F%E1%5C%3C%DE%24%D45%ADb%E7%40%82Y4%D6%D8%EC%B7nyP%ACW%0C%A5T%A9%3C%11%86%F4%EB%93N%F1%BE%A9%7F%A4j%F6%B" +
		"7ZN%AB%1D%B3%B4N.%22%90%17%8C%80G%CC%CA%01%3B%BD%C0%E8%A7%24%01%CDko%89%13ZX(%D4%06%9D%A9%CB%20-%14%D6%B7%0B%12%95%E8C%AC%98e9%07%F8y%15Rq%D9%E9%FA%9D4b%E2%B9%ADw%D3%CB%F49%9B%5Bm" +
		"f%0F%8BQ%1B%BB%D8%A6%9D%ED%CD%B3L%84%A9%F9b%C0%7C%FF%00%E3%D9%FC1%5Dt%0D%E2O%0F%EA6%BA%7D%A3%5C%EA%D6%D7w%3Ec%5C%B2%12%8A%AD%9C%85f'%00%01%BB%93%83%9E1%DE%9D%C7%C4%1DJ%EA%DA%7Bx%B" +
		"4%ED%3E%CFSH%8F%95ru%18J%A2%B0%0D%B8%06%EB%F2%E0%E3%DB%F0%AEv%1F%16%F8%9AK%8Bqq%E2Kx%E4G%FD%D4V%F1%A4%CD31%C6%0A%C66%9E%BD%C8%FC%EA%AB%E2%BD%A3%8F3%DBEe%D3%B1%B585%7BEY%EE%9E%B7%7" +
		"D%D5%B6%3D%A1L%BEh%0C%C0%A9RH%20%06C%EF%8E1%FE%1D%E8%AEG%C1w%D1%DB%5B%5EGy4%9Eu%C5%C9%B8i%A6e%CB%B3%05%04%90%B9X%FA%0C)%3D%08%F5%A2%94%AF-lp%B8%F2%BB%09%AD%F8b%5B%BD%9Eb4%BEQ%1E%5" +
		"C%F1%F2%C3%AFQ%83%D7%24%11%B5%95%BB%E3%B7%1Bu%E1%0B%C2B%B5%F4-%0A%E1%96%DE%E2%DDbX%9B%D9%15%BA%FA%F1%CFph%A2%B5Q%8DUy%A3zS%93%7B%8F%B5%F0%9C%CA%DB%C5%EE%9F%14%E7%E5i%A1%B4Rp%0FM%A" +
		"5%82s%EA%17%91%D7%A9%CE%DE%9F%E0%F8UB%97%B9%9E%22%B8%11(%DB%12%E7%AF%CA%9D%0F%24gvph%A2%AD%D1%A7OT%8D')r%DD%B3%AC%D3%B4%24%82%05%84%C3%1CP(%DB%E5(%03%8FN8%1F%D6%8A(%AC%1DI%3E%A73%" +
		"9Bg%FF%D9";
	return res;
}

var defaultSubjectIcons = initDefaultSubjectIcons();

var defaultOnClickEventRequiredForMessageTypeElement = initDefaultOnClickEventRequiredForMessageTypeElement();

var userSettingElements = initUserSettingsTable();
var userSettingValues = initUserSettingValues();
var userSettingsDisplayAbleValues;
var selectVectorValue = initSelectVectors();
var encodedImages = initEncodedImages();

var pagesToLoad = userSettingValues['NUMBER_OF_MESSAGES_ON_PAGE'] / 10;

var addressBookList = null;

var messageViewType = null;

var startValueRegexp = new RegExp('start=\\d{1,4}');
var endValueRegexp = new RegExp('end=\\d{1,4}');


var noticeDiv = null;
var mailRecipientDiv = null; 
var hiddenDataDiv = null;
var mailRecipients = null;
var myMailRecipients = null;

var url = self.location.href;
var existingMessageTableBody = null;
var paginatorLink = null;
var dummyTimeout;
var recipientCount = 1;
// You can change the number of max recipients, I limited in the towns count / island..
var NUMBER_OF_MAX_RECIPIENTS = 16;

var PLAYER_ALLY_DETAILS = GM_getValue( 'PLAYER_ALLY_DETAILS' );

if ( PLAYER_ALLY_DETAILS == null || PLAYER_ALLY_DETAILS.length == 0 )	{	
	requestMyDetails( function(responseDetails){savePlayerAllyDetails(responseDetails.responseText);} );
}

var userDefinedMenuOptions = null;	// keyword : option map
var userDefinedPicturePaths = null;	// keyword : path map

var userDefinedOptions = getUserDefinedIconsAndMenuOptions();
if ( userDefinedOptions != null )	{
	userDefinedMenuOptions = userDefinedOptions[0];
	userDefinedPicturePaths = userDefinedOptions[1];
}

var MESSAGE_PART_VARIABLE_SUFFIX = 'MESSAGE_PART_VARIABLE_SUFFIX'; 
